Use a covariant type for detectors argument#5135
Merged
xrmx merged 2 commits intoopen-telemetry:mainfrom Apr 30, 2026
Merged
Conversation
Contributor
|
Can you please add a Changelog entry. |
herin049
approved these changes
Apr 23, 2026
herin049
reviewed
Apr 23, 2026
This updates the type annotation for `get_aggregated_resources`'s
`detectors` argument to use `collections.abc.Sequence`, which is
covariant in its type parameter and thus has the expected behaviour for
an input type.
`typing.List` is invariant in its type parameter, which means that a
simple list of `ResourceDetector` subclasses is not a
`list[ResourceDetector]`.
Some typecheckers are more lenient than others, but [ty] enforces this
and thus the following code does not typecheck despite being
semantically correct.
from opentelemetry.sdk.resources import (
OsResourceDetector,
ProcessResourceDetector,
Resource,
get_aggregated_resources,
)
from opentelemetry.sdk.trace import TracerProvider
resource = Resource.create()
resource_detectors = [
ProcessResourceDetector(),
OsResourceDetector(),
]
tracer_provider = TracerProvider(
resource=get_aggregated_resources(
detectors=resource_detectors,
initial_resource=resource,
),
)
The full output of ty is as follows:
$ uv run ty check
error[invalid-argument-type]: Argument to function `get_aggregated_resources` is incorrect
--> test.py:18:9
|
18 | detectors=resource_detectors,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected `list[ResourceDetector]`, found `list[ProcessResourceDetector | OsResourceDetector]`
|
info: Function defined here
--> .venv/lib/python3.13/site-packages/opentelemetry/sdk/resources/__init__.py:507:5
|
507 | def get_aggregated_resources(
| ^^^^^^^^^^^^^^^^^^^^^^^^
508 | detectors: typing.List["ResourceDetector"],
| ------------------------------------------ Parameter declared here
|
info: `list` is invariant in its type parameter
info: Consider using the covariant supertype `collections.abc.Sequence`
info: For more information, see https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics
Found 1 diagnostic
[ty]: https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics
883ea90 to
262f275
Compare
Contributor
Author
|
This should be good to go. Let me know if I need to do anything else :) |
MikeGoldsmith
approved these changes
Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This updates the type annotation for
get_aggregated_resources'sdetectorsargument to usetyping.Sequence, which is covariant in its type parameter and thus has the expected behaviour for an input type.typing.Listis invariant in its type parameter, which means that a simple list ofResourceDetectorsubclasses is not alist[ResourceDetector].Some typecheckers are more lenient than others, but ty enforces this and thus the following code does not typecheck despite being semantically correct.
The full output of ty is as follows:
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
ty checkon the example given in the description with the updatedopentelemetry-sdkinstalled.Does This PR Require a Contrib Repo Change?
Checklist: